package com.qiyun.erp.auth.emp.business.ebo;

import com.qiyun.erp.auth.emp.business.ebi.EmpEbi;
import com.qiyun.erp.auth.emp.dao.dao.EmpDao;
import com.qiyun.erp.auth.emp.vo.EmpModel;
import com.qiyun.erp.auth.role.vo.RoleModel;
import com.qiyun.erp.util.base.BaseQueryModel;
import com.qiyun.erp.util.exception.AppException;
import com.qiyun.erp.util.format.MD5Util;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class EmpEbo implements EmpEbi {
    private EmpDao empDao;

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }

    public void save(EmpModel em) {
        //如果用户名没有输入，那么认定为特殊错误现象
        if (em.getUserName() == null || em.getUserName().trim().length() == 0) {
            //认定为错误现象
            throw new AppException("INFO_EMP_USERNAME_IS_EMPTY");
        }

        //对密码加密
        em.setPwd(MD5Util.md5(em.getPwd()));
        //设置默认值
        em.setLastLoginTime(System.currentTimeMillis());
        em.setLastLoginIp("-");
        em.setLoginTimes(0);
        empDao.save(em);
    }

    public void update(EmpModel em) {
    }

    public void delete(EmpModel em) {
        empDao.delete(em);
    }

    public EmpModel get(Serializable uuid) {
        return empDao.get(uuid);
    }

    public List<EmpModel> getAll() {
        return empDao.getAll();
    }

    public List<EmpModel> getAll(BaseQueryModel qm, Integer pageNum, Integer pageCount) {
        return empDao.getAll(qm, pageNum, pageCount);
    }

    public Integer getCount(BaseQueryModel qm) {
        return empDao.getCount(qm);
    }

    public EmpModel login(String userName, String pwd, String lastLoginIp) {
        //MD5加密
        pwd = MD5Util.md5(pwd);
        //调用数据层
        EmpModel loginEm = empDao.getByUserNameAndPwd(userName, pwd);
        if (loginEm != null) {
            //登录成功
            //添加登录信息
            //登录次数+1
            loginEm.setLoginTimes(loginEm.getLoginTimes() + 1);
            //最后登录时间
            loginEm.setLastLoginTime(System.currentTimeMillis());
            //最后登录IP
            loginEm.setLastLoginIp(lastLoginIp);
            //快照更新
        }
        return loginEm;
    }

    public boolean changePwd(String userName, String pwd, String newPwd) {
        //对密码加密
        pwd = MD5Util.md5(pwd);
        newPwd = MD5Util.md5(newPwd);
        return empDao.updatePwdByUserNameAndPwd(userName, pwd, newPwd);
    }

    @Override
    public void save(EmpModel em, Long[] roleUuids) {
        Set<RoleModel> roles = new HashSet<RoleModel>();
        for (Long uuid : roleUuids) {
            RoleModel temp = new RoleModel();
            temp.setUuid(uuid);
            roles.add(temp);
        }

        em.setRoles(roles);
        //对密码加密
        em.setPwd(MD5Util.md5(em.getPwd()));
        //设置默认值
        em.setLastLoginTime(System.currentTimeMillis());
        em.setLastLoginIp("-");
        em.setLoginTimes(0);
        empDao.save(em);
    }

    @Override
    public void update(EmpModel em, Long[] roleUuids) {
        EmpModel temp = empDao.get(em.getUuid());
        temp.setName(em.getName());
        temp.setEmail(em.getEmail());
        temp.setTele(em.getTele());
        temp.setGender(em.getGender());
        temp.setAddress(em.getAddress());
        temp.setDm(em.getDm());
        Set<RoleModel> roles = new HashSet<RoleModel>();
        for (Long uuid : roleUuids) {
            RoleModel temp2 = new RoleModel();
            temp.setUuid(uuid);
            roles.add(temp2);
        }
        temp.setRoles(roles);
    }
}
